package za.co.green;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;

public class UDPClientThread extends Thread {
	
	
	private String bindAddr = "";
    
    private int udpPort = 0;
    
    private int tcpPort = 0;
	
	public UDPClientThread() throws IOException {
		this("UDPClientThread");
	 }
	
	public UDPClientThread(String bindAddr, int udpPort, int tcpPort) throws IOException {
		super("UDPClientThread");
		
		this.bindAddr = bindAddr;
		
		this.udpPort = udpPort;
		
		this.tcpPort = tcpPort;
	 }
	
	public UDPClientThread(String name) {
        super(name);
    }
	
	public void run() {
		
		MulticastSocket socket = null;
		
		InetAddress group = null;
		
		InetAddress ip = null;
		
		try {
			ip = InetAddress.getLocalHost();
			
			String myHostAddress = ip.getHostAddress();
			
			socket = new MulticastSocket(udpPort);
			
			group = InetAddress.getByName(bindAddr);
			socket.joinGroup(group);
	
			DatagramPacket packet;
			
			List<String> recvFiles = new ArrayList<String>();
			
			for (int i = 0; i < 200; i++) {
				
			    byte[] buf = new byte[256];
			    
			    packet = new DatagramPacket(buf, buf.length);
			    
			    socket.receive(packet);// not getting anything ?
			    
			    String received = new String(packet.getData());
			    
			    if (!packet.getAddress().getHostAddress().equals(myHostAddress))
			    {
			    	System.out.println("UDP CLIENT >>> MSG FROM UDP SERVER: " + received);
			    	
			    	if (recvFiles.contains(received)) {
			    		continue;
			    	}
			    	else {
			    		recvFiles.add(received);
			    	}
			    	
			    	// Try to connect to the Node that send this UDP Msg, with SocketChannel
			    	// This is the TCP CLIENT
			    	
			    	SocketChannel sChannel = SocketChannel.open();
			    	sChannel.configureBlocking(true);
			    	
			    	if (sChannel.connect(new InetSocketAddress(packet.getAddress().getHostAddress(), tcpPort))) {

			    		ObjectOutputStream oos =
			    				new ObjectOutputStream(sChannel.socket().getOutputStream());
			    		
			    		oos.writeObject(received);
			    		
			    		ObjectInputStream ois = 
			                     new ObjectInputStream(sChannel.socket().getInputStream());
			    		
			    		String s = (String)ois.readObject();
			    		System.out.println("UDP CLIENT >>> TCP CLIENT >>>  String is: '" + s + "'");
			    		
			    	} else {
			    		System.out.println("COULD NOT READ FROM TCP STREAM");
			    	}
			        
			        
			    } /*
			    else
			    {
			    	System.out.println("UDP CLIENT >>> MY ECHO: " + received);
			    } */
			    
			}
	
		} catch (Exception e) {
			e.printStackTrace();
			
		} finally {
			try {
				if (group != null && socket != null)
					socket.leaveGroup(group);
			} catch (IOException e) {}
			//socket.disconnect();
			if (socket != null)
				socket.close();
		}
		return;
	}

}
